package de.fhd.pms.dao;

import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Fahrt;
import de.fhd.pms.model.Kunde;
import de.fhd.pms.dao.FahrtDao;

/**
 * The Data access class for {@link Kunde} objects. DAO encapsulates all accesses 
 * to the data source, including construction and dismantling of the connection.
 * All Interaction with the database regarding the entity bean {@link Kunde} 
 * should be handled by this class! Its tasks:
 * <ul>
 * <li>set new objects of the class</li>
 * <li>modify or delete an object of the class</li>
 * <li>deliver all objects of the class</li>
 * <li>further functions in context to the class objects</li>
 * </ul>
 * 
 * @author Fischer, Neetzel & Six
 */
public class KundeDao extends HibernateDaoSupport {
	private FahrtDao fahrtDao;

	public void setFahrtDao(FahrtDao fahrtDao) {
		this.fahrtDao = fahrtDao;
	}
	
	//Für JUNIT-Testing erstellt
	/**
	 * Creates and saves a {@link Kunde} object.
	 * @param vorname
	 * @param nachname
	 * @param telefon
	 */
	public void create(String vorname, String nachname, String telefon) {
		Kunde k = new Kunde();
		k.setVorname(vorname);
		k.setNachname(nachname);
		k.setTelefon(telefon);
		save(k);
	}

	/**
	 * Returns a single {@link Kunde} object by its primary DB key
	 * 
	 * @param id
	 *            the primary key of {@link Kunde}
	 * @return a single {@link Kunde}
	 */
	public Kunde findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Kunde) template.get(Kunde.class, id);
	}

	/**
	 * Saves {@link Kunde} object specified by the parameter in the DB
	 * 
	 * @param kunde
	 *            is the object that'll be saved in the DB
	 * @return the object specified by the parameter
	 */
	public Kunde save(Kunde kunde) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(kunde);
		return kunde;
	}

	/**
	 * Deletes the specified {@link Kunde} object from the DB
	 * 
	 * @param kunde
	 *            the object to be deleted
	 * @throws KundeDeleteException
	 *             if the specified {@link Kunde} is included in a {@link Fahrt}
	 * 
	 */
//	EXCEPTION VERSUCH
//	public void delete(Kunde kunde) throws KundeDeleteException {
//		HibernateTemplate template = getHibernateTemplate();
//		Set<Fahrt> fahrtenVonAuto = fahrtDao.findByKunde(kunde);
//		if (fahrtenVonAuto.size() == 0) {
//			template.delete(kunde);
//		} else {
//			throw new KundeDeleteException();
//		}
//	}

//	ORIGINAL
	public void delete(Kunde kunde) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(kunde);
	}

	/**
	 * Returns all {@link Kunde} objects from the DB
	 * 
	 * @return a list of {@link Kunde} objects
	 */
	@SuppressWarnings("unchecked")
	public List<Kunde> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		@SuppressWarnings("rawtypes")
		List results = template.loadAll(Kunde.class);
		return results;
	}

}
